{include file="public/header" /}
<body>
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
      <div class="layui-form-item">
        <div class="layui-inline">
          注：发布文章不得超出《中华人民共和国网络安全法》、《互联网信息服务管理办法》等国家明文规定的法律法规的范畴，如产生任何不良影响，后果由企业自负。
        </div>
      </div>
    </div>
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <!-- <button class="layui-btn layuiadmin-btn-list" data-type="batchdel">删除</button> -->
        <button class="layui-btn layuiadmin-btn-list layui-count" data-type="add">添加</button>
        <button class="layui-btn layuiadmin-btn-list layui-count" data-type="list">新闻管理</button>
      </div>
      <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
      <script type="text/html" id="buttonTpl">
        {{#  if(d.status == 1){ }}
        <button class="layui-btn layui-btn-xs">显示</button>
        {{#  } else { }}
        <button class="layui-btn layui-btn-primary layui-btn-xs">隐藏</button>
        {{#  } }}
      </script>
      <script type="text/html" id="table-content-list">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>
<script>
  layui.config({
    base: '__config__/' //静态资源所在路径
    ,version: new Date().getTime()
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table','form'], function(){
    var table = layui.table
            ,table = layui.table
            ,form = layui.form;
    table.render({
      elem: '#LAY-app-content-list'
      ,url: "{:url('news/categoryList')}"
      ,cols: [[
        {field: 'id',type: 'checkbox', align: 'center'}
        ,{field: 'name', minWidth: 400, title: '分类名称', align:'center'}
        ,{field: 'sort', title: '排序', align:'center', sort: true}
        ,{field: 'create_time', title: '创建时间', align:'center', sort: true}
        ,{field: 'status', title: '状态', templet: '#buttonTpl', minWidth: 80, align: 'center'}
        ,{title: '操作', minWidth: 200, align: 'center', toolbar: '#table-content-list'}
      ]]
      ,page: true
      ,limit: 15
      ,limits: [15, 20, 25, 30]
      ,text: {
        none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
      }
      ,done: function(res, curr, count){
        //如果是异步请求数据方式，res即为你接口返回的信息。
        //console.log(res);    //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
        //console.log(curr);  //得到当前页码
        //console.log(count); //得到数据总量
      }
    });
    //监听工具条，编辑（查看），删除
    table.on('tool(LAY-app-content-list)', function(obj){
      var data = obj.data;
      var category_id = data.id;
      if(obj.event === 'del'){
        layer.confirm('确定删除该分类吗？', {title : '温馨提示'}, function(index){
          $.ajax({
            url:"{:url('news/categoryDelete')}",
            type:'post',
            data:{'id':category_id},
            success: function(res){
              if(res.code == 0){
                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                layer.msg(res.msg,{icon:1});
              } else{
                layer.msg(res.msg,{icon:5});
              }
            }
          });
        });
      } else if(obj.event === 'edit'){
        var index = layer.open({
          type: 2
          ,title: '编辑分类'
          ,content: "{:url('news/categoryInfo')}?id=" + data.id
          ,maxmin: true
          ,area: ['400px', '350px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
            //监听提交
            iframeWindow.layui.form.on('submit(layuiadmin-app-form-edit)', function(data){
              var field = data.field; //获取提交的字段
              if(field.status){
                status = 1;
              } else {
                status = 0;
              }
              //提交 Ajax 成功后，静态更新表格中的数据
              $.ajax({
                url:"{:url('news/categoryEdit')}",
                type:'post',
                data:{'field':field,'status':status},
                success:function(res){
                  if(res.code == 0){
                    layer.msg(res.msg,{icon:1});
                    layer.close(index); //关闭弹层
                    table.reload('LAY-app-content-list');
                  }
                  else{
                    layer.msg(res.msg,{icon:5});
                    return false;
                  }
                }
              });
              form.render();
            });
            submit.trigger('click');
          }
        });
        //layer.full(index);  //全屏展示
      }
    });

    var $ = layui.$, active = {
      batchdel: function(){ //批量删除，暂时关闭
        var checkStatus = table.checkStatus('LAY-app-content-list')
                ,checkData = checkStatus.data; //得到选中的数据
        var id = new Array(); //声明数组
        for(var i in checkData) {
          id[i] = checkData[i].id;  //得到复选框的值
        }
        if(checkData.length === 0){
          layer.tips('请选择需要删除的行', this, {
            tips: [3, '#000'],
            time: 1500
          });
        } else{
          layer.confirm('确定删除吗？', function(index) {
            //执行 Ajax 后重载
            $.ajax({
              url:"{:url('news/categoryDelete')}",
              type:'post',
              data:{'id':id},
              success:function(res){
                if(res.code == 0){
                  layer.msg(res.msg,{icon:1});
                  location.reload();
                } else{
                  layer.msg(res.msg,{icon:5});
                }
              }
            });
          });
        }
      },
      add: function(){
        layer.open({
          type: 2
          ,title: '添加分类'
          ,content: "{:url('news/categoryInfo')}"
          ,maxmin: true
          ,area: ['400px', '300px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
            //监听提交
            iframeWindow.layui.form.on('submit(layuiadmin-app-form-submit)', function(data){
              var field = data.field; //获取提交的字段
              if(field.status){
                status = 1;
              } else {
                status = 0;
              }
              if(field.icon == ''){ layer.msg('模板不能为空',{icon:5});}
              //提交 Ajax 成功后，静态更新表格中的数据
              $.ajax({
                url:"{:url('news/categoryEdit')}",
                type:'post',
                data:{'status':status, 'field':field},
                success:function(res){
                  if(res.code == 0){
                    layer.msg(res.msg,{icon:1});
                  }
                  else{
                    layer.msg(res.msg,{icon:5});
                    return false;
                  }
                }
              });
              layer.close(index); //关闭弹层
              table.reload('LAY-app-content-list'); //重载表格
            });
            submit.trigger('click');
          }
        });
      }
      ,list: function(){
        window.location.href = "{:url('news/index')}";
      }
    };
    //监听删除，添加
    $('.layui-btn.layuiadmin-btn-list').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
    //刷新当前页面
    $('#LAY-app-contlist-refresh').click('on', function () {
      location.reload()
    });
  });
</script>
</body>
</html>